import 'dart:collection';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/res.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/network/response/BaseResp.dart';
import 'package:huajianshe_app/utils/toast_utils.dart';
import 'package:huajianshe_app/widget/RoundedCornerWidget.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';
import 'package:huajianshe_app/widget/StarWidget.dart';

import 'model/CircleModel.dart';


//给圈子打分
class CircleStarDialog extends StatefulWidget {

  final int? circleId;
  final String? title;
  final List<CircleLabel>? labels;

  const CircleStarDialog(this.circleId, this.title, this.labels, {Key? key}) : super(key: key);


  @override
  State<StatefulWidget> createState() {
    return _CircleStarDialogState();
  }
}

class _CircleStarDialogState extends BaseState<CircleStarDialog> {
  TextEditingController editingController = TextEditingController();
  FocusNode inputNode = FocusNode();
  late ValueNotifier<double> score0, score1, score2;

  @override
  void initState() {
    super.initState();
    score0 = ValueNotifier(0);
    score1 = ValueNotifier(0);
    score2 = ValueNotifier(0);
  }
  @override
  Widget build(BuildContext context) {
    print('circleId: ${widget.circleId}');
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: LayoutBuilder(builder: (context, constraints) {
          return SingleChildScrollView(
              child: ConstrainedBox(constraints: BoxConstraints(minWidth: constraints.maxWidth, minHeight: constraints.maxHeight),
                child: IntrinsicHeight(
                    child: Column(
                        children: [
                          Spacer(),
                          RoundedCornerWidget(backgroundColor: Colors.white, radius: 13, borderWidth: 0, child: Column(crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(height: 45, child: Row(
                                    children: [
                                      W(15),
                                      Text('给圈子评分', style: TS(15, C.gray1A)),
                                      Spacer(),
                                      SmartButton(width: 20, height: 20, drawableLeft: LoadImage('ic_close'), backgroundColor: Colors.transparent, borderColor: Colors.transparent,
                                          onPressed: () { AppNavigator.pop(context); }),
                                      W(15)
                                    ]
                                )),
                                D(),
                                H(13),

                                Padding(padding: EdgeInsets.only(left: 15), child: Text('给${widget.title}标签打分', style: TS(12, C.grayC3))),
                                H(8),
                                if ((widget.labels?.length ?? 0) > 0)
                                  Padding(padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                                    child: Row(children: [
                                      Text('${widget.labels?[0].name}', style: TS(13, C.gray666),),
                                      Spacer(),
                                      ValueListenableBuilder(valueListenable: score0, builder: (BuildContext context, double value, Widget? child) {
                                        return StarWidget(5, 18, 9, true, current: score0,);
                                      })
                                    ])
                                  ),
                                if ((widget.labels?.length ?? 0) > 1)
                                  Padding(padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                                      child: Row(children: [
                                        Text('${widget.labels?[1].name}', style: TS(13, C.gray666),),
                                        Spacer(),
                                        ValueListenableBuilder(valueListenable: score1, builder: (BuildContext context, double value, Widget? child) {
                                          return StarWidget(5, 18, 9, true, current: score1,);
                                        })
                                      ])
                                  ),
                                if ((widget.labels?.length ?? 0) > 2)
                                  Padding(padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                                      child: Row(children: [
                                        Text('${widget.labels?[2].name}', style: TS(13, C.gray666),),
                                        Spacer(),
                                        ValueListenableBuilder(valueListenable: score2, builder: (BuildContext context, double value, Widget? child) {
                                          return StarWidget(5, 18, 9, true, current: score2,);
                                        })
                                      ])
                                  ),
                                Padding(padding: EdgeInsets.symmetric(vertical: 12, horizontal: 15),
                                    child: TextField(autofocus: false, controller: editingController, focusNode: inputNode,
                                      maxLines: 5, maxLength: 100,
                                      decoration: InputDecoration(
                                        isDense: true,
                                        contentPadding: EdgeInsets.only(left: 15, right: 15, top: 10, bottom: 10),
                                        fillColor: C.grayF6,
                                        filled: true,
                                        border: new OutlineInputBorder(
                                          borderSide: BorderSide.none,
                                          borderRadius: const BorderRadius.all(Radius.circular(9.0)),
                                        ),
                                        hintStyle: TextStyle(color: Color(0xffcccccc)),
                                        hintText: "说点什么吧",
                                      ),
                                    )
                                ),
                                SmartButton(text: '提交评分', fontSize: 15, height: 44, width: double.infinity, radius: 13, cornerPosition: 12, onPressed: star,)
                              ]
                          ))
                        ]
                    )
                ),
              )
          );
        })
    );
  }

  star() { // todo
    List<Score> scores = [];
    print('${widget.labels}');
    if ((widget.labels?.length ?? 0) > 0) {
      scores.add(Score(widget.labels![0].id, score0.value));
    }
    if ((widget.labels?.length ?? 0) > 1) {
      scores.add(Score(widget.labels![1].id, score1.value));
    }
    if ((widget.labels?.length ?? 0) > 2) {
      scores.add(Score(widget.labels![2].id, score2.value));
    }
    print('${json.encode(scores)}');

    var data = {
      'circle_id': widget.circleId,
      'advise': editingController.text,
      'score_data': json.encode(scores)
    };
    sendReq(Api.circleScore(), BaseResp(), data: data);
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    if (t is BaseResp) {
      if (t.code == 0) AppNavigator.pop(context);
      Toast.show(t.msg);
    }

  }
}

class Score {
  int? id;
  double? score;

  Score(this.id, this.score);

  Map<String, dynamic> toJson() => {
    'id': id,
    'score': score,
  };
}